﻿using CommunityToolkit.Mvvm.Input;
using NoteMvvmApp.Models;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Windows.Input;

namespace NoteMvvmApp.ViewModels;

public partial class MainWindowViewModel : ViewModelBase
{
    public ObservableCollection<Folder> Folders { get; set; } = new ObservableCollection<Folder>();
    private Note _selectedNote;

    public Note SelectedNote
    {
        get => _selectedNote;
        set
        {
            _selectedNote = value;
            OnPropertyChanged(nameof(SelectedNote));
        }
    }

    public ICommand NewFolderCommand { get; }
    public ICommand NewNoteCommand { get; }
    public ICommand SaveNoteCommand { get; }

    public MainWindowViewModel()
    {
        NewFolderCommand = new RelayCommand(CreateNewFolder);
        NewNoteCommand = new RelayCommand(CreateNewNote);
        SaveNoteCommand = new RelayCommand(SaveNote);

        // 初始化数据
        var folder = new Folder { Name = "我的笔记" };
        folder.Notes.Add(new Note { Title = "笔记1", Content = "内容1" });
        folder.Notes.Add(new Note { Title = "笔记2", Content = "内容2" });
        folder.Notes.Add(new Note { Title = "笔记3", Content = "内容3" });
        Folders.Add(folder);

        // 默认选择第一个文件夹的第一个笔记
        if (folder.Notes.Count > 0)
        {
            SelectedNote = folder.Notes[0];
        }
    }

    private void CreateNewFolder()
    {
        Folders.Add(new Folder { Name = "新文件夹" });
    }

    private void CreateNewNote()
    {
        if (SelectedNote != null)
        {
            var note = new Note { Title = "新笔记" };
            SelectedNote.Notes.Add(note);
            SelectedNote = note;
        }
    }

    private void SaveNote()
    {
        // 保存笔记的逻辑
    }

}
